<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + " ://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<jsp:include page="root_public.jsp"/>
<title>修改管理员 - 管理员管理</title>
</head>

<body>
<div class="x-body">
    <form class="layui-form" action="<%=path%>/user/save">
        <div class="layui-form-item" style="position:absolute;top: -1000px;">
            <div class="layui-input-inline">
                <input type="text" id="ids" name="id" autocomplete="off" class="layui-input ids"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="name" class="layui-form-label">
                <span class="x-red">*</span>姓名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="name" name="name" required="" lay-verify="username"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="phone" class="layui-form-label">
                <span class="x-red">*</span>手机
            </label>
            <div class="layui-input-inline">
                <input type="text" id="phone" name="phone" required="" lay-verify="phone"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="email" class="layui-form-label">
                <span class="x-red">*</span>邮箱
            </label>
            <div class="layui-input-inline">
                <input type="text" id="email" name="email" required="" lay-verify="email"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="remarks" class="layui-form-label">
                角色选择
            </label>
            <div class="layui-input-inline roleList">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="remarks" class="layui-form-label">
                备注
            </label>
            <div class="layui-input-inline">
                <input type="text" id="remarks" lay-verify="remarks" name="remarks"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="" class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="update" lay-submit="">
                修改
            </button>
        </div>
    </form>
</div>
<script>

    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;

        // 获取角色列表
        var $roleList = $('.roleList');
        $.ajax({
            type: "post",
            url: "<%=path%>/role/findList",
            async: true,
            dataType: "json",
            success: function (data) {
                $roleList.html("");
                var data = data.data.roleList;
                console.log(data)
                for (var i = 0; i < data.length; i++) {
                    var $html = $('<input type="radio" name="role.id" value="' + data[i].id + '" title="' + data[i].name
                        + '">');
                    $roleList.append($html);
                }
                form.render();
            }
        });


        var str = location.search;
        str = str.substring(str.indexOf("=") + 1);
        /*
        * 获取权限详情
        * */
        $.ajax({
            type: "post",
            url: "<%=path%>/user/bs/findDetail",
            async: true,
            dataType: "json",
            data: {
                "id": str
            },
            success: function (data) {
                var data = data.data;
                $('#name').val(data.name);
                $('#no').val(data.no);
                $('#email').val(data.email);
                $('#phone').val(data.phone);
                $('#ids').val(data.id);
                $('#remarks').val(data.remarks);
                var role = data.role.id;
                $('input[type="radio"]').each(function () {
                    if ($(this).val() === role) {
                        $(this).attr("checked", true);
                        form.render();
                    }
                });
            }
        });

        //自定义验证规则
        form.verify({
            username: function (value) { //value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '用户名不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '用户名首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '用户名不能全为数字';
                }
                if (value.length < 3) {
                    return '用户名不能小于3位';
                }
                if (value.length > 10) {
                    return '用户名不能大于10位';
                }
            }
        });

        //监听提交
        form.on('submit(update)', function (data) {
            parent.location.replace(parent.location.href);
            parent.countAllNum("");
            var index = parent.layer.getFrameIndex(window.name);
            //关闭当前frame
            parent.layer.close(index);
            //发异步，把数据提交给php
            parent.layer.msg("增加成功", {icon: 6, time: 600});
        });
    });

</script>
</body>

</html>